<button type="submit" class="btn btn-primary">${preferences['text']}</button>
<script>
    $(function () {
        var containerId = "#container-${WidgetRequest.getContainerContext().id}";
        var form = $(containerId).closest("form");
        form.submit(function (e) {
            e.preventDefault();
            e.stopPropagation();
            var result = form.validationEngine('validate');
            if (result) {
                //TODO do some loading
                $.when($(".smart-widget").trigger("sp.submit")).then(function () {
                    var values = {};
                    var fieldMap = SP.serializeMap(form);
                    form.find(".smart-widget").each(function () {
                        var _this = $(this);
                        var _id = _this.attr("data-content-id");
                        var _fieldName = "persistent_field_" + _id;
                        if (fieldMap[_fieldName]){
                            values[_id] = fieldMap[_fieldName];
                        }
                    });
                    var actionUrl = form.attr("action");
                    $.ajax({
                        url: actionUrl,
                        type: "post",
                        data: JSON.stringify(values),
                        dataType: "json",
                        contentType: "application/json",
                        processData: false,
                        success: function (recordId) {
                            var url = "${@()}?layoutId=${layoutId}";
                            if (recordId > 0) {
                                url += "&recordId=" + recordId;
                            } else {
                                alert("请先创建数据绑定,才可提交数据.");
                                location.href = "${@("/sp/dataBinding")}?containerId=${containerId}"
                            }
                            location.href = url;
                        }
                    });
                }, function () {
                    alert("保存失败,请检查输入值的长度是否超出字段长度")
                });
            }
        });
    });
</script>